﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigUnitCtrlViewModel : BaseViewModel<ConfigUnitCtrlViewModel>
    {
        #region Variables

        private string _seachValue = "";
        private ObservableCollection<ConfigUnitsModel> _displayConfigUnitsModels = new ObservableCollection<ConfigUnitsModel>();

        #endregion

        #region Constructor

        public ConfigUnitCtrlViewModel()
        {
        
        }

        #endregion

        #region Properties


        public ObservableCollection<ConfigUnitsModel> DisplayConfigUnitsModels
        {
            get
            {
                _displayConfigUnitsModels.Clear();
                List<ConfigUnitsModel> lst = DataManager.Instance.ConfigUnitsModelList.ToList().Where(O => O.Name.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigUnitsModels.Add(item);
                }
                return _displayConfigUnitsModels;
            }

            set
            {
                _displayConfigUnitsModels = value;
                RaisePropertyChanged("DisplayConfigUnitsModels");
            }
        }


        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigUnitsModels");
            }
        }

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigUnitsModelService.Instance.Update(sender as ConfigUnitsModel);
        }
        private void AddConfigUnitsModel(ConfigUnitsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigUnitsModelService.Instance.Update(obj);
                ConfigUnitDataGridViewModel.Instance.ConfigUnitsModel.Add(obj);
            }
            else ConfigUnitsModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigUnitsModel(ConfigUnitsModel configUnitsModel)
        {
            if (configUnitsModel != null)
            {
                ConfigUnitDataGridViewModel.Instance.ConfigUnitsModel.Remove(configUnitsModel);
                RaisePropertyChanged("DisplayConfigUnitsModels");
                ConfigUnitsModelService.Instance.DeleteById(configUnitsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigUnitsModel model = new ConfigUnitsModel();
            DisplayConfigUnitsModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigUnitsModel(sender as ConfigUnitsModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion
    }
}
